<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Stat extends Stats_controller {

	function __construct()
	{
		parent::__construct();
		date_default_timezone_set('PRC');
	}
	public function index()
	{
		$data['start'] = date('Y-m-d',time()-3600*24);
		$data['end'] = date('Y-m-d',time());
		$db = $this->mongo_db->get_db();
		$query = array( "last_activity" => array( "\$gt" => 0, "\$lte" => 12932267000 ) );
		$map = '
		    function() {
		        emit({ip_address:this.ip_address},{msum:this.count});
		    }
		';
		
		$reduce = '
		    function(key, values) {
		        var ret = {msum:0,abc:0};
		        for (var i=0;i<values.length;i++) {
		            ret.msum += values[i].msum;
		        }      
		        return ret;
		    }
		';
		
		$finalize = '
			function(key,val){
				return val;
			}
		';
		$out = '\''.time().'\'';
		$result = $db->command(array(
		    'mapreduce' => 'smt_visitor_logs',
		    'query'		=> $query,
		    'out'		=> $out,
		    'map'       => $map,
		    'reduce'    => $reduce,
		    'finalize'	=> $finalize,
		));
		$tempcollection = $result['result'];
		$result = $db->{$tempcollection}->find();
		foreach($result as $doc):
	 		$returns[] = $doc;
	 	endforeach;
	 	$db->{$tempcollection}->drop();
	 	$this->mongo_db->close();
		debug($returns);
		
		$this->load->view('panel',$data);
		//$this->mongo_db->limit(10)->get("smt_visitor_logs");
	}
	
	public function daily()
	{
		/***********************按天统计归档*****************************/
		//起始天数 $start $end n年n月n号
		$start = $this->input->get('start');
		$end = $this->input->get('end');
		$start_timestamp = strtotime($start);
		$end_timestamp = strtotime($end)+3600*24;
		debug(date('Y-m-d H:i:s',$end_timestamp));
		//pv
		$this->mongo_db->where_between_ne($start_timestamp,$end_timestamp);
		
		//uv
		//ip
		/*********************按天统计归档结束****************************/
		
		/***********************按小时统计归档***************************/
		//pv
		//uv
		//ip
		/*********************按小时统计归档结束**************************/
	}
}
